package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.dao.AssignmentAllocationStats;
import com.example.stuwork.pojo.Assignment;
import com.example.stuwork.service.AssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 作业管理控制器
 */
@RestController
@RequestMapping("/assignments")
public class AssignmentController {
    
    @Autowired
    private AssignmentService assignmentService;
    
    /**
     * 获取所有作业
     */
    @GetMapping("")
    public Result<List<Assignment>> getAllAssignments() {
        return assignmentService.getAllAssignments();
    }
    
    /**
     * 根据ID获取作业详情
     */
    @GetMapping("/{id}")
    public Result<Assignment> getAssignmentById(@PathVariable("id") Long assignmentId) {
        return assignmentService.getAssignmentById(assignmentId);
    }
    
    /**
     * 根据教师ID获取作业列表
     */
    @GetMapping("/teacher/{teacherId}")
    public Result<List<Assignment>> getAssignmentsByTeacherId(@PathVariable("teacherId") String teacherId) {
        return assignmentService.getAssignmentsByTeacherId(teacherId);
    }
    
    /**
     * 根据教师ID、课程ID和班级ID获取作业列表
     */
    @GetMapping("/teacher/{teacherId}/course/{courseId}/class/{classId}")
    public Result<List<Assignment>> getAssignmentsByTeacherAndCourseAndClass(
            @PathVariable("teacherId") String teacherId,
            @PathVariable("courseId") String courseId,
            @PathVariable("classId") Long classId) {
        return assignmentService.getAssignmentsByTeacherAndCourseAndClass(teacherId, courseId, classId);
    }
    
    /**
     * 根据课程ID和班级ID获取作业列表
     */
    @GetMapping("/course/{courseId}/class/{classId}")
    public Result<List<Assignment>> getAssignmentsByCourseAndClass(
            @PathVariable("courseId") String courseId,
            @PathVariable("classId") Long classId,
            @RequestParam(value = "studentId", required = false) String studentId) {
        if (studentId != null && !studentId.isEmpty()) {
            return assignmentService.getAssignmentsByCourseAndClassForStudent(courseId, classId, studentId);
        }
        return assignmentService.getAssignmentsByCourseAndClass(courseId, classId);
    }
    
    /**
     * 添加作业
     */
    @PostMapping("")
    public Result<Assignment> addAssignment(@RequestBody Assignment assignment, 
                                           @RequestParam(value = "studentIds", required = false) List<String> studentIds) {
        return assignmentService.addAssignment(assignment, studentIds);
    }
    
    /**
     * 更新作业
     */
    @PutMapping("/{id}")
    public Result<Assignment> updateAssignment(@PathVariable("id") Long assignmentId, 
                                             @RequestBody Assignment assignment) {
        assignment.setAssignmentId(assignmentId);
        return assignmentService.updateAssignment(assignment);
    }
    
    /**
     * 删除作业
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAssignment(@PathVariable("id") Long assignmentId) {
        return assignmentService.deleteAssignment(assignmentId);
    }
    
    /**
     * 获取作业提交统计信息
     */
    @GetMapping("/{id}/stats")
    public Result<AssignmentAllocationStats> getAssignmentStats(@PathVariable("id") Long assignmentId) {
        return assignmentService.getAssignmentStats(assignmentId);
    }
    
    /**
     * 检查作业标题是否已存在
     */
    @GetMapping("/check-title")
    public Result<Boolean> checkAssignmentTitleExists(
            @RequestParam("courseId") String courseId,
            @RequestParam("classId") Long classId,
            @RequestParam("title") String title) {
        return assignmentService.checkAssignmentTitleExists(courseId, classId, title);
    }
    
    /**
     * 获取即将截止的作业
     */
    @GetMapping("/upcoming")
    public Result<List<Assignment>> getUpcomingDeadlines(@RequestParam(value = "days", defaultValue = "7") int days) {
        return assignmentService.getUpcomingDeadlines(days);
    }
    
    /**
     * 获取作业详情并附带提交统计
     */
    @GetMapping("/{id}/with-stats")
    public Result<Map<String, Object>> getAssignmentWithStats(@PathVariable("id") Long assignmentId) {
        return assignmentService.getAssignmentWithStats(assignmentId);
    }
} 